package com.zmovie.admin.controller.user;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmovie.admin.constant.Constants;
import com.zmovie.admin.controller.vo.OrderVO;
import com.zmovie.admin.controller.vo.SessionVO;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.*;
import com.zmovie.framework.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 用户订单管理
 * @since 2023-12-06 15:59
 */
@Controller
@Api(tags = "用户订单管理")
@RequestMapping("/user/order")
public class UserOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private SessionService sessionService;
    @Autowired
    private CinemaService cinemaService;
    @Autowired
    private HallService hallService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieRuntimeService movieRuntimeService;

    @GetMapping
    @ApiOperation("用户订单管理页面")
    public String index(Model model){
        Page<Order> orderPage = orderService.pageOrder(Integer.valueOf(Constants.PAGE_NO), Integer.valueOf(Constants.PAGE_SIZE), new Order());
        model.addAttribute(Constants.ORDER_PAGE_KEY, convert(orderPage));
        return "user/user_order";
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取用户订单")
    @ResponseBody
    public Object getOrder(@PathVariable("id") Integer id){
        return CommonReturnType.success(orderService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除订单")
    @ResponseBody
    public Object deleteOrder(@PathVariable("id") @NotNull Integer id){
        orderService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除订单")
    @ResponseBody
    public Object deleteOrderBatch(Integer[] id) throws BusinessException {
        if(ArrayUtil.isEmpty(id)){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        orderService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索")
    public String search(Order order, Integer pageNo, Integer pageSize, Model model){
        Page<Order> orderPage = orderService.pageOrder(pageNo, pageSize, order);
        model.addAttribute(Constants.ORDER_PAGE_KEY, convert(orderPage));
        return "user/user_order :: orderList";
    }
    private Page<OrderVO> convert(Page<Order> orderPage){
        Page<OrderVO> orderVOPage = new Page<>();
        BeanUtils.copyProperties(orderPage, orderVOPage);
        //转VO
        List<OrderVO> orderVOS = orderPage.getRecords().stream().map(this::convert).collect(Collectors.toList());
        orderVOPage.setRecords(orderVOS);
        return orderVOPage;
    }
    private OrderVO convert(Order order){
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);
        //用户
        User user = userService.getById(order.getUserId());
        orderVO.setUser(user);
        //场次
        Session session = sessionService.getById(order.getSessionId());
        orderVO.setSessionVO(convert(session));
        return orderVO;
    }
    private SessionVO convert(Session session){
        SessionVO sessionVO = new SessionVO();
        BeanUtils.copyProperties(session, sessionVO);
        //电影院
        Cinema cinema = cinemaService.getById(session.getCinemaId());
        sessionVO.setCinema(cinema);
        //影厅
        Hall hall = hallService.getById(session.getHallId());
        sessionVO.setHall(hall);
        //电影
        Movie movie = movieService.getById(session.getMovieId());
        sessionVO.setMovie(movie);
        //电影播放时段
        MovieRuntime movieRuntime = movieRuntimeService.getById(session.getMovieRuntimeId());
        sessionVO.setMovieRuntime(movieRuntime);
        return sessionVO;
    }
}
